
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContentOverContainer.Common;
using ContentOverContainer.Common.Converter;
using ContentOverContainer.SimpleSyntax;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers.Common;
using Roslyn.Services;
using Roslyn.Services.Formatting;

namespace ContentOverContainer
{



    /// <summary>
    /// Builds a list of declaration nodes annotated with the MultiplicityAttribute together with their replacements
    /// and a list of their references.
    /// </summary>
    /// <author>Harald Binkle</author>
    public class FieldWalker : BaseWalker
    {
        public FieldWalker(ISolution pSolution, ReferencesList pReferences, ChangeNodeInfoList pChangeNodePairs) : base(pSolution, pReferences, pChangeNodePairs)
        {
        }

        public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            FieldDeclarationSyntax syntaxNode = node;
            if (syntaxNode == null) return;            
            //check if field is annotated by getting the list of multiplicity attributes
            var multiplicityAtt = node.AttributeLists
                                      .GetMultiplicityAttributes();
            if (multiplicityAtt == null)
                return;
            //if annotated get the MultiplicityInfo object
            var varDecl = node.Declaration;
            var multiInfo = multiplicityAtt.GetMultiplicityInfo(node.Declaration.Type);            
            //find all references and add them to the list
            var references = varDecl.FindAllReferences(
                            this.m_SemModel, this.m_Solution).Cast<SyntaxNode>();
            m_References.Add(references, multiInfo, Document);
            //changes the type of the field (in case of Any annotation)
            var newnode = node.ChangeToMultiplicityType(multiInfo);
            //turn annotation into comment
            var changeNode = newnode.RemoveMultiplicityAttribute(
                                            newnode.AttributeLists, true);
            //add the new changed field to the list of nodes to be replaced at the end
            m_Replacements.Add(node, changeNode, Document);
        }

    }
}
